import axios from 'axios';
let require =axios.create({
 baseURL:"http://127.0.0.1:3000",
 timeout:5000,
})
require.interceptors.request.use((config)=>{
 let token=localStorage.getItem('token');
config.headers.Authorization=`Bearer ${token}`
return config;
},error=>{
if(error&&error.response){
switch(error.response.status){
 case 401:
 error.message='未授权，请重新登录'
 break;
case 403:
error.message='拒绝访问'
break;
case 404:
error.message='请求错误，未找到该资源'
 window.location.href='/NotFound'
  break;
case 405:
error.message='请求方法未允许'
 break;
case 500:
error.message='服务器端出错'
 break;
 default:
error.message=`连接错误${error.response.status}`
 }
}else{
 if(JSON.stringify(error).includes('timeout')){
console.log('服务器响应超时，请稍后再试');
 }
 error.message='连接服务器失败'
 }
return Promise.resolve(error.response)
})
require.interceptors.response.use((res)=>{
 return res.data;
})
export default require;